package com.yunbiao.ybbusstopplate.utils;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.yunbiao.ybbusstopplate.R;

import java.io.File;

public class GlideLoadUtils {
    private static GlideLoadUtils glideLoadUtils;
    private GlideLoadUtils(){}
    public static GlideLoadUtils getInstance(){
        if (glideLoadUtils==null)
            glideLoadUtils=new GlideLoadUtils();
        return glideLoadUtils;
    }

    public void loadLocalImage(final Context context, String path, final ImageView targetView, final View bgView, final String animationId){
        //添加图片内存管理
        Glide.with(context).clear(targetView);
        Glide.with(context).load(new File(path)).skipMemoryCache(false).dontAnimate().error(R.color.color_w).listener(new RequestListener<Drawable>() {
            @Override
            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                return false;
            }

            @Override
            public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                Animation animation = getImageLoadAnimation(context,animationId);
                if (animation != null) {
                    animation.setAnimationListener(new Animation.AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {
                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            if (bgView != null) {
                                bgView.setBackgroundDrawable(targetView.getDrawable());
                            }
                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {
                        }
                    });

                    targetView.startAnimation(animation);

                } else {
                    try {
                        bgView.setBackgroundDrawable(targetView.getDrawable());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                return false;
            }
        }).into(targetView);
    }

//    private int count=0;
    public void loadLocalImage(final Context context, String path, final ImageView targetView, final View bgView, final String animationId,final BitmapRecycle bitmapRecycle){
        //添加图片内存管理
        Glide.with(context).clear(targetView);
        final Object drawable =targetView.getDrawable();
        //添加图片内存管理
        bitmapRecycle.addBitmap(null,bgView);
        Glide.with(context).load(new File(path)).skipMemoryCache(false).dontAnimate().error(R.color.color_w).listener(new RequestListener<Drawable>() {
            @Override
            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                return false;
            }

            @Override
            public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                Animation animation = getImageLoadAnimation(context,animationId);
                if (animation != null) {
                    animation.setAnimationListener(new Animation.AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {
                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            if (bgView != null) {
                                bgView.setBackgroundDrawable(targetView.getDrawable());
                            }
                            bitmapRecycle.recycleBitmaps();
                            //若之前就为null，则加载之后再添加
                            if (drawable==null){
                                bitmapRecycle.addBitmap(null,bgView);
                            }
                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {
                        }
                    });

                    targetView.startAnimation(animation);
                } else {
                    try {
                        bgView.setBackgroundDrawable(targetView.getDrawable());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    bitmapRecycle.recycleBitmaps();
                    //若之前就为null，则加载之后再添加
                    if (drawable==null){
                        bitmapRecycle.addBitmap(null,bgView);
                    }
                }
                return false;
            }
        }).into(targetView);
    }

    private Animation getImageLoadAnimation(Context context,String animationId) {
        int playType = Integer.valueOf(animationId);
//        Log.e("animation.playType", "playType:" + playType);
        Animation imagePlayType = null;
        if (playType > 0) {
            imagePlayType = AnimationUtils.loadAnimation(context, AnimationInterface.imagePlayList[playType - 1]);
        }
        return imagePlayType;
    }

    interface AnimationInterface {
        int[] imagePlayList = new int[]{
                R.anim.push_up_in,
                R.anim.zoomin,
                R.anim.slide_in_from_left,
                R.anim.slide_in_from_right,
                R.anim.translate,
                R.anim.alpha_rotate,
                R.anim.scale_rotate};
    }
}
